import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import os
from google.appengine.ext.webapp import template



class MainPage(webapp.RequestHandler):
    
    
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'index.html')

        self.response.out.write(template.render(path, {}))
        

    def post(self):
        
        content = self.request.get('content')
        
        if(content.strip().startswith('<html>')):
            self.response.out.write(content)
        else:
            self.response.out.write(content.replace('\n', '<br>'))


    def login(self):
        self.redirect(users.create_login_url(self.request.uri))
        
            
    def parse_url(self):
        split_path = self.request.path.lower().split('/')
        split_qs = self.request.query_string.split('=')
        
        levels = list()
        for level in split_path :
            if level != '' :
                levels.append(level)
                
        if len(levels) > 2 :
            self.response.out.write('excesso!')            

        hash = ''
        if(len(split_qs) == 2 and split_qs[0] == 'h'):
            hash = split_qs[1]
        
        return hash, levels
    
    
class Pagina(db.Model):
    page_name = db.StringProperty()
    content = db.TextProperty()
    date = db.DateTimeProperty()
    user = db.UserProperty()
    hash = db.StringProperty()
         
                    
        



application = webapp.WSGIApplication([('/.*', MainPage)], debug=True)


def main():
    run_wsgi_app(application)


if __name__ == "__main__":
    main()
